package com.zhny.aiot.led.four.screen.plugin.factory.imp;

import com.zhny.aiot.led.four.screen.plugin.config.LedConfig;
import com.zhny.aiot.led.four.screen.plugin.factory.AbstractCmdFactory;
import com.zhny.aiot.led.four.screen.plugin.factory.ILedCmdFactory;
import com.zhny.aiot.led.four.screen.plugin.model.LedCommand;
import com.zhny.aiot.led.four.screen.plugin.model.LedContent;
import com.zhny.aiot.led.four.screen.plugin.model.LedModifyParam;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import org.pf4j.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LedModifyCmdFactoryImpl extends AbstractCmdFactory<LedModifyParam> {
    protected final Logger logger = LoggerFactory.getLogger(LedModifyCmdFactoryImpl.class);
    private int currentPage = 1;
    public LedModifyCmdFactoryImpl(LedConfig config, ILedCmdFactory factory){
        super(config,factory);
    }

    @Override
    protected Class<LedModifyParam> supportCmdParam() {
        return LedModifyParam.class;
    }

    @Override
    protected String cmdMethodName() {
        return "modify";
    }

    @Override
    public int getCmd() {
        return 0x29;
    }

    @Override
    public LedCommand handler(String method,String json) throws UnsupportedEncodingException {
        if(method.equals(cmdMethodName())) {
            LedModifyParam param=acceptJson(json);
            if(param != null) {
                return onEncode(param);
            }else return null;
        }else {
            return next(method,json);
        }
    }

    @Override
    protected LedCommand onEncode(LedModifyParam modifyParam) throws UnsupportedEncodingException {
        if(config == null)
            throw new NullPointerException("led config param is null ! please check !");

        int showPage = (currentPage + 1) > modifyParam.getTotalPage() ? 0: currentPage + 1;
        currentPage++;
        LedContent con = modifyParam.getContentList ().get(showPage);
        if(StringUtils.isNullOrEmpty(con.getContent()))
            throw new NullPointerException("Led content is null !");

        byte[] tempBuf = con.getContent().getBytes("gb2312");
        byte[] contentBytes = convertGB2312Bytes (tempBuf);
        if(contentBytes != null || contentBytes.length > 0) {
            logger.info("led content data {}", ByteBufUtil.hexDump(contentBytes));
            ByteBuf contentBuf= Unpooled.wrappedBuffer(contentBytes);
            if(contentBuf != null) {
                logger.info("start led encode modify command !");
                int contentSize=contentBuf.readableBytes();
                ByteBuf buf=Unpooled.buffer(64);
                buf.writeShortLE (config.getId());
                buf.writeByte(1);
                buf.writeByte(2);
                buf.writeByte(0);
                buf.writeByte(modifyParam.getFontColor());
                buf.writeShortLE (contentSize);
                buf.writeBytes(contentBuf);
                logger.info("led modify command [{}]", ByteBufUtil.hexDump(buf));
                return new LedCommand(config,getCmd(),buf);
            }
        }
        return null;
    }

    private byte[] convertGB2312Bytes(byte[] buf){
        ByteArrayOutputStream output = new ByteArrayOutputStream ();
        for(int i = 0; i < buf.length;i++){
            if(buf[i]>=32 && buf[i]<=126){
                output.write (0);
            }
            output.write (buf[i]);
        }
        return output.toByteArray ();
    }
}
